import { post, get } from "@/api/axios"
import type { BiuJsonResult } from "@/api/biu-json-result"

const urlPrefix: string = "/api/http-https-proxy"

export interface HttpHttpsProxyState {
  port: number
  mode: string
  serverId: string
}

export interface ConnectionRule {
  id?: string
  content: string
  method: string
  action: string
  createTime: number
  updateTime: number
}

export function doQueryState() {
  return get<BiuJsonResult<HttpHttpsProxyState>>(urlPrefix + "/state")
}

export function doChangeService(port: number, mode: string, serverId?: string) {
  return post<BiuJsonResult<boolean>>(
    urlPrefix + `/change-service?port=${port}&mode=${mode}&serverId=${serverId}`
  )
}

export function doQueryConnectionRule() {
  return get<BiuJsonResult<Array<ConnectionRule>>>(urlPrefix + "/query-connection-rule")
}

export function doDeleteConnectionRule(id: string) {
  return post<BiuJsonResult<boolean>>(urlPrefix + "/delete-connection-rule?id=" + id)
}

export function doUpsertConnectionRule(rule: ConnectionRule) {
  return post<BiuJsonResult<boolean>>(urlPrefix + "/upsert-connection-rule", rule)
}
